using System.Collections.Generic;
using Life.UI.MVC.Controller;
using UnityEngine;

namespace Life.UI.MVC
{
    [System.Serializable]
    public class Proxy
    {
        public int LayerID;
        public RectTransform LayerTarget;
    }
    
    /// <summary>
    /// 作用是定义、组织、管理UI的层级关系
    /// </summary>
    public class LayerRoot : MonoBehaviour
    {
        [SerializeField] private bool userCanvasLayer;
        [SerializeField] Proxy[] layerProxy;
        /// <summary>
        /// UI的Layer最大层数
        /// </summary>
        public const int MaxLayerCount
#if LAYER_COUNT_20
            = 20;
#elif LAYER_COUNT_30
            = 30;
#elif LAYER_COUNT_40
            = 40;
#elif LAYER_COUNT_50
            = 50;
#else
            = 10;
#endif
        /// <summary>
        /// UI每层的最大Order数
        /// </summary>
        public const int MaxOrderCount
#if ORDER_COUNT_30
            = 30;
#elif ORDER_COUNT_40
            = 40;
#elif ORDER_COUNT_50
            = 50;
#elif ORDER_COUNT_60
            = 60;
#else
            = 20;
#endif

        private class Layer: IComparer<ControllerBase>
        {
            public GameObject LayerObj;
            public List<ControllerBase> Controllers = new List<ControllerBase>(4);
            public void AddController(ControllerBase controller)
            {
                int index = Controllers.BinarySearch(controller, this);
                if (index < 0) index = ~index;
                Controllers.Insert(index, controller);
                controller.transform.SetParent(LayerObj.transform, false);
                controller.transform.localScale = Vector3.one;
                controller.transform.localPosition = Vector3.zero;
                controller.transform.SetSiblingIndex(index);
                if (!controller.gameObject.activeSelf)
                {
                    controller.gameObject.SetActive(true);
                }
            }
            public int Compare(ControllerBase x, ControllerBase y)
            {
                return x.UiOrder.CompareTo(y.UiOrder);
            }
        }

        private Layer[] _layers = new Layer[MaxLayerCount];
        private bool _initialized = false;
        /// <summary>
        /// 将UI的Controller添加至对应的Layer
        /// </summary>
        /// <param name="controller"></param>
        public void AddController(ControllerBase controller)
        {
            if (!_initialized)
            {
                foreach (var proxyItem in layerProxy)
                {
                    _layers[proxyItem.LayerID] = new Layer()
                    {
                        LayerObj = proxyItem.LayerTarget.gameObject
                    };
                }
                _initialized = true;
            }
            
            if (_layers[controller.UILayer] == null || _layers[controller.UILayer].LayerObj == null)
            {
                Layer layer = new Layer//创建新的Layer
                {
                    LayerObj = new GameObject($"Layer_{controller.UILayer}"),//创建Layer的GameObject
                };
                //设置Layer的RectTransform
                RectTransform rectTrans = layer.LayerObj.AddComponent<RectTransform>();
                rectTrans.SetParent(transform, false);
                rectTrans.localScale = Vector3.one;
                rectTrans.localPosition = Vector3.zero;
                rectTrans.anchorMin = Vector2.zero;
                rectTrans.anchorMax = Vector2.one;
                rectTrans.offsetMin = Vector2.zero;
                rectTrans.offsetMax = Vector2.zero;
                _layers[controller.UILayer] = layer;
                int count = 0;
                for (int i = 0; i < controller.UILayer; ++i)
                {
                    if (_layers[i] != null)
                    {
                        count++;
                    }
                }
                rectTrans.SetSiblingIndex(count);
            }
            _layers[controller.UILayer].AddController(controller);
        }
    }
}

